package com.orhon.smartcampus.modules.student.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.LearninfoDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Learninfo;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudentLearnInfo;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;
import com.orhon.smartcampus.modules.student.mm.LearninfoMapper;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.UserTypeUtil;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;


@Component
public class LearninfosFetcher implements DataFetcher {
	@Autowired
	LearninfoDao learninfoDao;


	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();
		List<GQLStudentLearnInfo> GQLStudentLearnInfo = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from Learninfo where 1=1 "+ UserTypeUtil.decide(token);;
		
		if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("id")!=null) {
				hql += " and id " + Util.decide(map.get("id"));
			}
			if(map.get("user_id")!=null) {
				hql += " and user_id " + Util.decide(map.get("user_id"));
			}
			if(map.get("student_id")!=null) {
				hql += " and student_id " + Util.decide(map.get("student_id"));
			}
			if(map.get("school_id")!=null) {
				hql += " and school_id " + Util.decide(map.get("school_id"));
			}
			if(map.get("arrive_id")!=null) {
				hql += " and arrive_id " + Util.decide(map.get("arrive_id"));
			}
			if(map.get("transaction_type")!=null) {
				hql += " and transaction_type " + Util.decide(map.get("transaction_type"));
			}
			if(map.get("membership_number")!=null) {
				hql += " and membership_number " + Util.decide(map.get("membership_number"));
			}
			if(map.get("class_code")!=null) {
				hql += " and class_code " + Util.decide(map.get("class_code"));
			}
			if(map.get("start_year")!=null) {
				hql += " and start_year " + Util.decide(map.get("start_year"));
			}
			if(map.get("finished_year")!=null) {
				hql += " and finished_year " + Util.decide(map.get("finished_year"));
			}
			if(map.get("admission_method")!=null) {
				hql += " and admission_method " + Util.decide(map.get("admission_method"));
			}
			if(map.get("way_to_annex")!=null) {
				hql += " and way_to_annex " + Util.decide(map.get("way_to_annex"));
			}
			if(map.get("student_from")!=null) {
				hql += " and student_from " + Util.decide(map.get("student_from"));
			}
			if(map.get("confirmed")!=null) {
				hql += " and confirmed " + Util.decide(map.get("confirmed"));
			}
			if(map.get("confirmed_by")!=null) {
				hql += " and confirmed_by " + Util.decide(map.get("confirmed_by"));
			}

            if(map.get("way_to_study")!=null) {
                hql += " and way_to_study " + Util.decide(map.get("way_to_study"));
            }
            if(map.get("at_school")!=null) {
                hql += " and at_school " + Util.decide(map.get("at_school"));
            }
            if(map.get("deleted_at")!=null) {
            	hql += " and deleted_at " + Util.decide(map.get("deleted_at"));
            }
            
		}
		List<Learninfo> list = learninfoDao.find(hql,null,page.getPage(),page.getLimit());


		for (Learninfo item : list) {
			GQLArrives gqlArrives = null;
			if (env.getSelectionSet().contains("data/arrives")){
				Arrives arrives = item.getStudentArrivesByArriveId();
				gqlArrives = ArrivesMapper.MAPPER.toArrives(arrives);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			GQLStudent gqlStudent = null;
			if (env.getSelectionSet().contains("data/student")){
				SInformation information = item.getStudentSInformationByStudentId();
				gqlStudent = StudentMapper.MAPPER.toStudent(information);
			}

			GQLUser GQLUser = null;
			if (env.getSelectionSet().contains("data/user")){
				Users users = item.getUserUsersByUserId();
				GQLUser = UserMapper.MAPPER.mapUser(users);
			}

			GQLStudentLearnInfo learnInfo = LearninfoMapper.MAPPER.toLearninfo(item);
			learnInfo.setUser(GQLUser);
			learnInfo.setSchool(gqlSchool);
			learnInfo.setArrive(gqlArrives);
			learnInfo.setStudent(gqlStudent);
			GQLStudentLearnInfo.add(learnInfo);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = learninfoDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLStudentLearnInfo).put("total", total);
	}


}
